/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets.patient.profile;

import be.ugent.medirem.logic.PatientRemote;
import be.ugent.medirem.persistence.entity.details.LocationDetails;
import be.ugent.medirem.persistence.entity.details.PatientDetails;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Thomas Roelens
 */
public class ProfileServlet extends HttpServlet {

    @EJB
    private PatientRemote patientBean;

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String username = request.getUserPrincipal().getName();
        Long id = patientBean.getPatientId(username);
        try {
            PatientDetails p = patientBean.getPatientDetails(id);
            LocationDetails l = patientBean.getLocationDetails(id);

            request.setAttribute("patient", p);
            request.setAttribute("location", l);
            request.setAttribute("patientid", id);

        } catch (Exception ex) {
            Logger.getLogger(ProfileServlet.class.getName()).log(Level.SEVERE, null, ex);
            request.getSession().setAttribute("flash", "An error occured when getting your patient information!");
        }

        request.getRequestDispatcher("/patient/profile/show.jsp").forward(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
